package com.example.haohao.myview;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import android.os.Bundle;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


public class MyViewMainActivity extends AppCompatActivity {

    private FengChe fengche;


    /**
     * 开始
     */
    static final   int What_0=0;
    /**
     * 结束
     */
    static final   int What_1=1;
     Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final FengChe taiJi = new FengChe(this);
        setContentView(taiJi);
//        setContentView(R.layout.activity_main);

//        try {
//            // 第三个参数必须为：com.tencent.stat.common.StatConstants.VERSION
//            StatService.startStatService(this, "A8I47TG9ZJGV",
//                    com.tencent.stat.common.StatConstants.VERSION);
//            Log.d("MTA","MTA初始化成功");
//            Toast.makeText(this, "MTA初始化成功", Toast.LENGTH_SHORT).show();
//        } catch (MtaSDkException e) {
//            // MTA初始化失败
//            Log.d("MTA","MTA初始化失败"+e);
//        }


//        fengche = (FengChe) findViewById(R.id.fengche);
        // 创建 ObjectAnimator 对象
// 执行动画


//       new Thread(new Runnable() {
//           @Override
//           public void run() {
//               fengche.animate().rotation(1080).setDuration(5000);
//           }
//       }).start();





        handler = new Handler() {
            private float degrees = 0;
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what==What_0) {
                    taiJi.setRotate(degrees += 5);
                    if (handler!=null) {
                        sendEmptyMessageDelayed(0, 20);
                    }

                }else if (msg.what==What_1){
                    handler=null;
                }

            }
        };
        if (handler!=null) {
            handler.sendEmptyMessageDelayed(0, 20);
        }
    }

    private void setRotate(FengChe fengche, float v) {
        RotateAnimation rotate = new RotateAnimation(0, v, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

//        rotate.setDuration(2000);//动画时间


        fengche.setAnimation(rotate);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (handler!=null) {

            handler.sendEmptyMessage(What_1);
        }
    }
}
